<?php

class myTools
{
  public static function normalizeTag($tag)
  {
    $n_tag = mb_strtolower($tag);
 
    // remove all unwanted chars
    $n_tag = preg_replace('/[^a-zA-Z0-9\.-]/', '', $n_tag);
    // remove double dots and dashes
    $n_tag = preg_replace('/[\.-]{2,}/', '-', $n_tag);
 
    return $n_tag;
  }
 
  public static function splitPhrase($phrase)
  {
    $tags = array();
    $token = " \n\t\r";
    
    $tok = strtok( $phrase , $token );
 
    while( $tok ){
	    
	    $tag = self::normalizeTag($tok);
	    
	    if( $tag ){
		    if( !in_array( $tag , $tags )){
			    $tags[] = $tag;
		    }
	    }
	    
	    $tok = strtok( $token );
    }
 
    return array_unique($tags);
  }
  
  public static function makeHandle($string)
  {
    $handle = mb_convert_case(strip_tags($string), MB_CASE_LOWER);
    $handle = mb_ereg_replace('[\W]', ' ', $handle);
    $handle = mb_eregi_replace('[\s]{2,}', ' ', $handle);
    $handle = mb_substr($handle, 0, 200);
    $handle = mb_eregi_replace('[\s]', '-', trim($handle));

    return $handle;
  }
}
